#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode * left;
    TreeNode * right;
    TreeNode() : val(0), left(nullptr), right(nullptr)
    {}
    TreeNode(int val) : val(val), left(nullptr), right(nullptr)
    {}
    TreeNode(int val, TreeNode * left, TreeNode * right) : val(val), left(left), right(right)
    {}
};

class Solution 
{
    void recursion(TreeNode * cur)
    {
        if(cur->left == nullptr && cur->right == nullptr)
        {
            return;
        }
        swap(cur->left, cur->right);
        if(cur->left != nullptr)
        {
            recursion(cur->left);
        }
        if(cur->right != nullptr)
        {
            recursion(cur->right);
        }
    }
public:
    TreeNode* invertTree(TreeNode* root) 
    {
        if(root == nullptr)
        {
            return root;
        }
        recursion(root);
        return root;
    }
};

// 辅助函数：创建二叉树节点
TreeNode* createNode(int val) {
    return new TreeNode(val);
}

// 辅助函数：层序遍历打印二叉树（用于验证结果）
vector<int> levelOrderTraversal(TreeNode* root) {
    vector<int> result;
    if (root == nullptr) return result;
    
    queue<TreeNode*> q;
    q.push(root);
    
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        
        if (node != nullptr) {
            result.push_back(node->val);
            q.push(node->left);
            q.push(node->right);
        } else {
            result.push_back(-1); // 用-1表示null节点
        }
    }
    
    // 移除末尾的null节点
    while (!result.empty() && result.back() == -1) {
        result.pop_back();
    }
    
    return result;
}

// 辅助函数：打印数组结果
void printResult(const vector<int>& result) {
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        if (result[i] == -1) {
            cout << "null";
        } else {
            cout << result[i];
        }
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (root == nullptr) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main() {
    Solution solution;
    
    // 测试案例1: LeetCode示例1 - [4,2,7,1,3,6,9]
    //       4
    //      / \
    //     2   7
    //    / \ / \
    //   1  3 6  9
    // 翻转后: [4,7,2,9,6,3,1]
    cout << "测试案例1: [4,2,7,1,3,6,9]" << endl;
    TreeNode* root1 = createNode(4);
    root1->left = createNode(2);
    root1->right = createNode(7);
    root1->left->left = createNode(1);
    root1->left->right = createNode(3);
    root1->right->left = createNode(6);
    root1->right->right = createNode(9);
    
    cout << "翻转前: ";
    printResult(levelOrderTraversal(root1));
    
    TreeNode* result1 = solution.invertTree(root1);
    cout << "翻转后: ";
    printResult(levelOrderTraversal(result1));
    cout << "预期: [4,7,2,9,6,3,1]" << endl << endl;
    
    // 测试案例2: LeetCode示例2 - [2,1,3]
    //   2
    //  / \
    // 1   3
    // 翻转后: [2,3,1]
    cout << "测试案例2: [2,1,3]" << endl;
    TreeNode* root2 = createNode(2);
    root2->left = createNode(1);
    root2->right = createNode(3);
    
    cout << "翻转前: ";
    printResult(levelOrderTraversal(root2));
    
    TreeNode* result2 = solution.invertTree(root2);
    cout << "翻转后: ";
    printResult(levelOrderTraversal(result2));
    cout << "预期: [2,3,1]" << endl << endl;
    
    // 测试案例3: LeetCode示例3 - []
    cout << "测试案例3: []" << endl;
    TreeNode* root3 = nullptr;
    
    cout << "翻转前: ";
    printResult(levelOrderTraversal(root3));
    
    TreeNode* result3 = solution.invertTree(root3);
    cout << "翻转后: ";
    printResult(levelOrderTraversal(result3));
    cout << "预期: []" << endl << endl;
    
    // 测试案例4: 只有一个节点 - [1]
    cout << "测试案例4: [1]" << endl;
    TreeNode* root4 = createNode(1);
    
    cout << "翻转前: ";
    printResult(levelOrderTraversal(root4));
    
    TreeNode* result4 = solution.invertTree(root4);
    cout << "翻转后: ";
    printResult(levelOrderTraversal(result4));
    cout << "预期: [1]" << endl << endl;
    
    // 测试案例5: 左偏树 - [1,2,null,3]
    //   1
    //  /
    // 2
    ///
    //3
    // 翻转后: [1,null,2,null,3]
    cout << "测试案例5: 左偏树 [1,2,null,3]" << endl;
    TreeNode* root5 = createNode(1);
    root5->left = createNode(2);
    root5->left->left = createNode(3);
    
    cout << "翻转前: ";
    printResult(levelOrderTraversal(root5));
    
    TreeNode* result5 = solution.invertTree(root5);
    cout << "翻转后: ";
    printResult(levelOrderTraversal(result5));
    cout << "预期: [1,null,2,null,3]" << endl << endl;
    
    // 测试案例6: 右偏树 - [1,null,2,null,3]
    // 1
    //  \
    //   2
    //    \
    //     3
    // 翻转后: [1,2,null,3]
    cout << "测试案例6: 右偏树 [1,null,2,null,3]" << endl;
    TreeNode* root6 = createNode(1);
    root6->right = createNode(2);
    root6->right->right = createNode(3);
    
    cout << "翻转前: ";
    printResult(levelOrderTraversal(root6));
    
    TreeNode* result6 = solution.invertTree(root6);
    cout << "翻转后: ";
    printResult(levelOrderTraversal(result6));
    cout << "预期: [1,2,null,3]" << endl << endl;
    
    // 测试案例7: 不对称树
    //       1
    //      / \
    //     2   3
    //    /     \
    //   4       5
    // 翻转后: [1,3,2,5,null,null,4]
    cout << "测试案例7: 不对称树 [1,2,3,4,null,null,5]" << endl;
    TreeNode* root7 = createNode(1);
    root7->left = createNode(2);
    root7->right = createNode(3);
    root7->left->left = createNode(4);
    root7->right->right = createNode(5);
    
    cout << "翻转前: ";
    printResult(levelOrderTraversal(root7));
    
    TreeNode* result7 = solution.invertTree(root7);
    cout << "翻转后: ";
    printResult(levelOrderTraversal(result7));
    cout << "预期: [1,3,2,5,null,null,4]" << endl << endl;
    
    // 清理内存
    deleteTree(result1);
    deleteTree(result2);
    deleteTree(result4);
    deleteTree(result5);
    deleteTree(result6);
    deleteTree(result7);
    // result3 是 nullptr，不需要删除
    
    return 0;
}